<?php
	require_once("setup.inc.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script language="JavaScript" type="text/JavaScript">
//ajax base
function createRequest() {
  var request = null;
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = null;
      }
    }
  }

  if (request == null) {
    alert("Error creating request object!");
  } else {
    return request;
  }
}
function makeRequest(url,toWhom) {
	http_request = createRequest();
	
	http_request.onreadystatechange = toWhom;
	http_request.open('GET', url, true);
	http_request.send(null);
}


function editField(name,text,spanName,formType) {
	var object = document.getElementById(spanName);
	var newContents;
	newContents = '<input type="text" class="editInput" name="' + name + '" id="ajaxUpdateInput_'+name+'" value="' + text + '"><input type="button" id="ajaxUpdateButton_'+name+'" value="Save" onClick="javascript:saveField(\'' + name + '\',document.getElementById(\'ajaxUpdateInput_' + name + '\').value,\''+spanName+'\',1)">';
	object.innerHTML = newContents;
	document.getElementById("ajaxUpdateInput_"+name).select();
}
function saveField(name,text,spanName,formType) {
	//do ajax post here
	var updateButton = document.getElementById("ajaxUpdateButton_" + name);
	var updateInput  = document.getElementById("ajaxUpdateInput_" + name);
	updateInput.disabled = true;
	updateButton.disabled = true;
	updateButton.value = 'Saving...';
	
	http_request = createRequest();	
	http_request.onreadystatechange = function(){updateField(name,text,spanName,1,http_request)};
	http_request.open('GET', "includes/ajax.php?updateField="+name+"&value="+text, true);
	http_request.send(null);
}
function updateField(name,text,spanName,formType,requestObj) {
	if (requestObj.readyState == 4) {
		if (requestObj.status == 200) {
			responseXML = requestObj.responseXML.documentElement;
			var responseError = responseXML.getElementsByTagName('error')[0].firstChild.data;
			var responseMsg = responseXML.getElementsByTagName('msg')[0].firstChild.data;
			var responseValue = responseXML.getElementsByTagName('value')[0].firstChild.data;
			if(responseError == 0) {
				//update successful
				var responseResult = responseXML.getElementsByTagName('result')[0].firstChild.data;			
				var object = document.getElementById(spanName);
				var newContents;
				newContents = responseResult + '<span class="ajaxEditLink">[ <a href="javascript:editField(\''+name+'\',\'' + responseResult + '\',\''+spanName+'\',1)">EDIT</a> ]</span>';
				object.innerHTML = newContents;
			} else {
				//update unsuccessful. Tell them why, and set back to editField
				alert(responseMsg);
				editField(name,responseValue,spanName,1);
			}
		} else {
			alert("An error occurred while saving your change!");
		}
	}
}
</script>
<style type="text/css">
<!--
* {
	font-family: Verdana, Arial, Helvetica, sans-serif;
}
.ajaxEditLink {
	font-size:10px; 
	font-weight:bold; 
	padding-left:5px;
}
table#editInfo {
	font-size:12px;
}
-->
</style>
</head>
<body>
<div style="color:#990000; font-weight:bold ">It is important that the user never knows any information about the database!</div>
<table id="editInfo">
  <tr>
    <td width="75" height="47">Name</td>
    <td width="251"><?= driverName($_SESSION['driver_id']) ?></td>
  </tr>
  <tr>
    <td>Email</td>
    <td><?php writeAjaxUpdateField('1b2e4a850f7951428f949bd381c2f25e',driverDetail($_SESSION['driver_id'],'email'),'edit_email'); ?></td>
  </tr>
  <tr>
    <td>Phone</td>
    <td><?php writeAjaxUpdateField('a3911fb0c6cb6cc857e2aed25f17e168',formatPhone(driverDetail($_SESSION['driver_id'],'cellphone')),'edit_phone'); ?></td>
  </tr>
</table>


</body>
</html>
